package edu.ptu.springboot04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.ptu.springboot04.domain.entity.User;
import edu.ptu.springboot04.utils.LoginUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @BelongsProject: springboot04
 * @BelongsPackage: edu.ptu.springboot04.service.impl
 * @Author: HYJ
 * @CreateTime: 2025-03-27  19:55
 * @Description: TODO
 * @Version: 1.0
 */
public class UserDetailServiceImpl implements UserDetailsService {

    @Resource
    private UserServiceImpl service;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(User::getUsername, username);
        User user = service.getOne(wrapper);

        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("username 不存在");
        }
        return  new LoginUser(user);
    }
}
